﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using GNSCore.Constants;
using GNSWebSite.Helpers;
using System.Threading;
using GNSWebSite.Settings;

namespace GNSWebSite.Managers
{
    /// <summary>
    /// For mail setting, sending mail
    /// </summary>
    public static class MailManager
    {
        /// <summary>
        /// Sends user created message to the user
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        public static void SendUserCreated(string userName, Guid userId, string password, string email)
        {
            MailMessage message = new MailMessage();
            message.To.Add(email);
            message.IsBodyHtml = true;
            //todo localize
            message.Subject = "Global Notification System";
            string link = SitePathHelper.GetBaseSitePath() + "Pages/Activation.aspx?id=" + userId.ToString();
            message.Body = string.Format(MailConstants.MailTemplate, userName, password, link);
            //todo change to new thread (read xml settings)
            SendMail(message);
            //Thread thread = new Thread(new ParameterizedThreadStart(SendMail));
            //thread.Start(message);
        }

        private static void SendMail(object message)
        {
            try
            {
                SmtpClient smtp = new SmtpClient
                                      {
                                          //                                    UseDefaultCredentials = true,
                                          EnableSsl = MailSettings.UseSSL
                                      };
                smtp.Send((MailMessage)message);
            }
            catch (Exception ex)
            {
                //todo write to the log
            }
        }

        public static void SendConfirmEmail(string userName, Guid useridentifier, string password, string email)
        {
            bool useSSL = MailSettings.UseSSL;
            MailMessage message = new MailMessage();
            message.To.Add(email);
            message.IsBodyHtml = true;
            //todo localize
            message.Subject = "Global Notification System";
            string link = SitePathHelper.GetBaseSitePath() + "Pages/Activation.aspx?id=" + useridentifier.ToString();
            message.Body = string.Format(MailConstants.MailTemplate, userName, password, link);
            Thread thread = new Thread(new ParameterizedThreadStart(SendMail));
            thread.Start(message);
        }
    }
}
